db = require '../utils/db'

module.exports = 
	'get /admin/blog': (req, res)->
		sql = "select id, title, create_time, `active` from blog order by create_time desc"
		db.runSql sql, (err, result)->
			res.send result

	'get /admin/blog/:id': (req, res)->
		sql = "select * from blog where id = ?"
		db.queryOne sql, [req.params.id], (err, result)->
			res.send result


	'post /admin/blog': (req, res)->
		req.body.update_time = new Date()
		sql = "insert into blog set ?"
		db.runSql sql, [req.body], (err, result)->
			res.send success: not err?

	'post /admin/blog/:id' : (req, res)->
		req.body.create_time = new Date(req.body.create_time)
		req.body.update_time = new Date()
		sql = "update blog set ? where id = ?"
		db.runSql sql, [req.body, req.params.id ], (err, result)->
			res.send success: not err?

	'del /admin/blog/:id': (req, res)->
		sql = "delete from blog where id = ?"
		db.runSql sql, [req.params.id], (err, result)->
			res.send success: not err?

